package ru.isys.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;

import ru.isys.activity.MainActivity;
import ru.isys.xml.XmlItem;

public class FormPanel extends JPanel implements HaveMainListener{

	private MainActivity listener;
	List<FormItem> elements;
	GridBagConstraints constraints;
	GridBagLayout gridbag;
	JButton ok;
	
	public FormPanel(final MainActivity listener) {
		setListener(listener);
		gridbag = new GridBagLayout();
		constraints = new GridBagConstraints();
		setLayout(gridbag);
		ok = new JButton("Хоккей");
		ok.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				System.out.println("OLOLOLOLOL this is form panel");
			    listener.onSaveForm();
			}
		});

	}

	public void setItems(List<FormItem> elements) {
		this.removeAll();
		this.elements = elements;
		for (FormItem currentItem : elements) {
			constraints.insets = new Insets(10, 10,
					10, 0);
			constraints.gridx = 0;
			gridbag.setConstraints(currentItem, constraints);
			add(currentItem);

		}
		constraints.insets = new Insets(10, 10, 10, 0);
		constraints.gridx = 0;
		gridbag.setConstraints(ok, constraints);
		add(ok);
		validate();
	}

	public void setData(XmlItem data) {
		setItems(elements);
	}

	@Override
	public void setListener(MainActivity listener) {
		this.listener = listener;
	}
	
	public List<FormItem> getFormItems(){
	    return this.elements;
	}

}
